#ifndef WBSECSVC_4_WAY_HANDSHAKE_INC__
#define WBSECSVC_4_WAY_HANDSHAKE_INC__

//#include "EapolKey.h"
//#include "sysdef.h"



//*********************************************************************
//Authentication Defintion
//*********************************************************************
//Added bty WangJS 2005.8.26
//To define the length and the type of Key
#define KEYTYPE_WEP40                       0
#define KEYTYPE_WEP104                      1
#define KEYTYPE_TKIP                        2 // 128 bit key
#define KEYTYPE_AES_CCMP                    3 // 128 bit key

#define OPEN_AUTH							0
#define SHARE_AUTH							1
#define ANY_AUTH							2
#define WPA_AUTH							3	//for WPA
#define WPAPSK_AUTH							4
#define WPANONE_AUTH						5

//Added by WangJS 2005.11.16
#define PRFSize                             6 + 6 + 32 + 32


//********************************************************************
//The struct is defined to add the privacy key
//Adde by WangJS 2005.9.20
//********************************************************************
typedef u8 MAC_ADDRESS[6];
//typedef unsigned char KEY_RSC[8];

typedef struct _AP_802_11_KEY
{
  u32       Length;
  u32       KeyIndex;
  u32       KeyLength;
  MAC_ADDRESS BSSID;
  //ULONGLONG   KeyRSC;
  u64     KeyRSC;
  u8       KeyMaterial[128];
} AP_802_11_KEY, *PAP802_11_KEY;


//Added by WangJS 2005.11.29

typedef struct _HEX_802_11_KEY
{
	u32	   KEYLength;
	u8	   KEYMaterial[128];
} HEX_802_11_KEY, *PHEX_802_11_KEY;
//*********************************************************************
///////////////////////////////////////////////////////////////////////
#define ReSendMaxNumber 2            //To define the value of re-send
								//Added by WangJS 2005.10.12
///////////////////////////////////////////////////////////////////////
//To define the meaning block
///////////////////////////////////////////////////////////////////////

typedef u8 Block8[8];
typedef u8 Block16[16];
typedef u8 Block24[24];
typedef u8 Block32[32];
typedef u8 Block64[64];


typedef u8 MACAddress[6];

//Added by WangJS 2005.12.8
#define MACAddressLength 6
///////////////////////////////////////////////////////////////////////
//The structure is defined Station Authentication state
//If the Station is associated  status, the station must process the 
//4-way handshake status
///////////////////////////////////////////////////////////////////////

enum {
	STA_DISASSOC=0,
	STA_ASSOCIATED_COMPLETION,
	STA_4WAY_HANDSHAKE
	
};

//To define the type of install key, added by WangJS 2005.9.29
enum {
	InstallPairwiseKey=0,
	InstallGroupKey
};

//To define the enumeration of 4-way
enum {
	FWayStep1=1,
	FWayStep2,
	FWayStep3,
	FWayStep4,
	GHSStep1,
	GHSStep2,
	//The states is for station
	STA_GHSSTtep2,
	STA_MAX_COUNT
};

///////////////////////////////////////////////////////////////////////
//To define the EAPoL Frame
typedef struct  _EAPoL_PACKET{
	u32  TotalSize;
	u32 EAPoLBuffer[4096];
}__attribute__ ((packed)) EAPoL_PACKET,pEAPoL_PACKET;
///////////////////////////////////////////////////////////////////////
//typedef void (*OnStepFn)(STATION* STA, PEAP_HEADER EapolMessage, u32 MessageSize);

typedef struct _PairMasterKey{
    Block8          m_ReplayCounter;
    MACAddress      m_AA;                //To retrieve the Authentication address
    Block32         m_ANonce;               //Nonce gnerated from Authentication
    MACAddress      m_SA;
    Block32         m_SNonce;
    Block16         m_KeyIV;
    Block16         m_KeyMIC;
    Block8          m_KeyRSC;
    Block32         m_pmk;
    Block16         m_mk;
    Block16         m_ek;
    Block16         m_tk1;
    Block16         m_tk2;
    Block32         m_KeyNonceSeed;

//	u8   m_OnStep;
	u16   m_OnStep;

//	EAPoL_PACKET    EAPoL_Packet;

}__attribute__ ((packed)) PairMasterKey, *pPairMasterKey;

////////////////////////////////////////////////////////////////////////////
//To retrieve information of the group key
//Added by WangJS 2005.10.7
typedef struct _GroupMasterKey{
	Block32         m_GNonce;               //Nonce generate by Group Handshake, added by WangJS 2005.9.28
	Block16         m_gtk1;
    Block16         m_gtk2;
}__attribute__ ((packed)) GroupMasterKey, *pGroupMasterKey;

////////////////////////////////////////////////////////////////////////////
//To retrieve information of the Station Status

typedef struct _STATION{
	MACAddress     STA_MAC_ADDRESS;      //To identify the STA 
//	unsigned char	       IsConnected;          //To retrieve the Connect status
	PairMasterKey  PMK;
	GroupMasterKey GMP;
//	u8		   ReSendNumber;         //Added by WangJS 2005.10.12
//	u32		   TimeStamp;
    
}__attribute__ ((packed)) STATION, *pSTATION;


/////////////////////////////////////////////////////////////////////////////
//To define the variable
/////////////////////////////////////////////////////////////////////////////
#define  pGlobal_STA_Wpa (&(Adapter->Global_STA_Wpa))
#define  pSTA (&(Adapter->Global_STA_Wpa.WPA_STA))
//**************************************************************************
    enum {
        WLAN_EID_RSN_WPA    = 0xDD,
        WLAN_EID_RSN_WPA2   = 0x30, 
    };

    enum {
    		WLAN_RSN_VERSION    = 0x0001,
	};

    enum {
        CIPHER_SUITE_NONE   = 0,
        CIPHER_SUITE_WEP40  = 1,
        CIPHER_SUITE_TKIP   = 2,
        CIPHER_SUITE_WRAP   = 3,
        CIPHER_SUITE_CCMP   = 4,
        CIPHER_SUITE_WEP104 = 5,
    };

    enum {
        AUTH_KEY_MGT_SUITE_RESERVED     = 0,
        AUTH_KEY_MGT_SUITE_UNSPECIFIED  = 1,
        AUTH_KEY_MGT_SUITE_NONE         = 2,
    };
 



//***********************************************************************
// To start the 4 Way Handshake
//***********************************************************************
  //void Start4WayHandshake(unsigned short StaState, MACAddress ReceievedStaMACAddress, unsigned int PacketSize, void* Packet);
   void Start4WayStateMachine(void* DeviceHandle, u32 PacketSize, void* Packet , u32 SendPacketSize, void* SendPacket);
//***********************************************************************
// EAPoL packet process
//***********************************************************************
    void AP_PasswordHash(void* DeviceHandle, /*const */u8* Password, Block32* PMK);
    //Added by WangJS 2005.12.12
    void AP_PrePasswordHash(void* DeviceHandle, u8* SSID, u8 SSIDLength);
    

//***********************************************************************
//The 4Way Handshake
//***********************************************************************
    	
    //Clear all state
    void FourWayClear(void* DeviceHandle);
    // Pairwise key generation functions
    void FourWayInit(void* DeviceHandle);

    void AP_OnStep2(void* DeviceHandle, STATION* sta, void* Eap, u32 MessageSize);

    //void AP_OnStep3(STATION* STA, void* Eap, unsigned int MessageSize);
    void AP_OnStep4(void* DeviceHandle, STATION* sta, void* Eap, u32 MessageSize);
       
	//For Group handshake of Station, Added by WangJS 2005.11.11
    void STA_OnGStep2(void* DeviceHandle, STATION* sta, void* Eap, u32 MessageSize);
	//The function can generate the pairwise key and group key
    void GenerateANonce(Block32* KeyNonceSeed);

	void GeneratePairwisePRF(u8* PairwisePRF,
							 Block32*		  pmk, 
							 MACAddress*	  AA, 
							 MACAddress*	  SA, 
							 Block32*		  ANonce, 
							 Block32*		  SNonce);

	//Generate GroupPRF, added by WangJS 2005.9.28
	    
	//Added by WangJS 2005.11.11, in order to fill the rsn element of station
	u32 Supplicat_FillRSN(void* DeviceHandle, u8* rsn, u32 rsn_len);

    u32 BuildEapolKeyMessage(void* DeviceHandle, u8* Buffer, u32 BufferSize,
							 MACAddress* DA, MACAddress* SA, 
							 u32 KeyDescVer, unsigned char bSecure, unsigned char bMIC, unsigned char bResponse, 
    
    unsigned char bPairwise, unsigned char bInstall, u32 KeyIndex,Block8* ReplayCount, Block8* KeyRSC, 
							 Block32* KeyNonce, Block16* MK, Block16* KeyIV,
							 u8* KeyData, u32 KeyDataSize);

    unsigned char KeyMICIsValid(void* DeviceHandle, PEAP_HEADER Eap, u32 Len,  Block16* MK, Block16* m_mk);

    unsigned char Is8021xPacket(u16 Type);
	
	//Added by WangJS 2005.9.27
    unsigned char IsEAPoL_KEY_FRAME(void* Eap, u32 Len);
	//////////////////////////////////////////////////////////////
	//Group Key Function
	//Added by WangJS 2005.9.28
	//////////////////////////////////////////////////////////////
	void GenerateGTK(GroupMasterKey* GMK);
	//////////////////////////////////////////////////////////////
	//void InstallKey(void* DeviceHandle, MACAddress* STA_MAC_Address, Block16* Key1,Block16* Key2, unsigned char KeyType);
	void InstallKey(void* DeviceHandle, MACAddress* STA_MAC_Address, Block16* Key1,Block16* Key2, unsigned char KeyType, u16 KeyIndex);
	
	//Added by WangJS 2005.11.28
	void FourWay_Get_Encryption_Status(void* DeviceHandle, u8 *EncryptStatus);
	//random function
	void srand(u32 seed);
	u32 rand(void);

	//cpu word
	void CpuWord(u16* w);
	u8 Compare(void* Byte1, void* Byte2, u32 ByteSize, u8 ComStr);
	
	//Added by WangJS 2005.12.23
	void TimeStamp(void* TagString);
	//Added by WangJS 2006.8.11
	//To check the packet of EAPoL frame is pairwise or group key
	u32 CheckEAPType(void* DeviceHandle,void* RecvPacket);
	//Added by WangJS 2006.8.22
	u8 CharToBin(u8 Character);

#endif // WBSECSVC_4_WAY_HANDSHAKE_INC__
// end of file


